function  [x,y] = display_flow_features(tuv,space,frames,colormap_type,minrs,maxrs)
% function display_flow_features(tuv,space,frames,colormap_type,minrs,maxrs)
% display flow field features using 'jet' colormap.
%       tuv - the flow field data. A  M*N*2 matrix
%       space - grid step-size of the flow field vectors
%       frames - frames extracted by flow_ip_detect.m
%       optional:
%           colormap_type - 'log' or 'linear'
%           minrs,maxrs
%
%
%
%
%
% This file is part of the source-code demonstrating the method published 
% in the paper: 
%
% Wei Liu and Eraldo Ribeiro. Scale and Rotation Invariant Detection 
%      of Singular Patterns in Vector Flow Fields. IAPR International 
%      Workshop on Structural Syntactic Pattern Recognition (S-SSPR), 
%      Cesne, Turkey, 2010.
%
% BIBTEX ENTRY:
%
% @inproceedings{WeiRibeiroSSPR2010,
%   author    = {Wei Liu and Eraldo Ribeiro},
%   title     = {Scale and Rotation Invariant Detection of Singular 
%                Patterns in Vector Flow Fields},
%   booktitle = {IAPR International Workshop on Structural Syntactic 
%                Pattern Recognition (S-SSPR)},
%   year      = {2010},
%   pages     = {XXX-XXX}
% }
%
% The source-code can be obtained from 
%        http://www.cs.fit.edu/~eribeiro/flowdetector/
% 
% You are free to use, change, or redistribute this code in any way you
% want for non-commercial purposes. However, it is appreciated if you 
% maintain the name of the original authors.
%
% (c) 2010 by Wei Liu and Eraldo Ribeiro 
% eribeiro@cs.fit.edu
% Florida Instititute of Technology, Melbourne, Florida, U.S.A.
%
if(nargin<=4)
    colormap_type='log';
end
spaced_quiver(tuv,space);
hold on;
[y,I]=sort(frames(3,:));

%rs=log(frames(4,:));
rs=frames(4,:);

if(nargin<5)
    minrs=min(rs);maxrs=max(rs);
end
cm=colormap('jet');

%% create the color map according to the responses of the features.
for ii=1:size(frames,2)
    tt=I(ii);
    radius=frames(3,tt);
    %rs=log(frames(4,tt));
    rs=frames(4,tt);
    x=frames(1,tt);
    y=frames(2,tt);
    x=x/space;
    y=y/space;
    radius=radius/space;
    if(strcmp(colormap_type,'linear'))
        c=linear_color_map(rs,minrs,maxrs,cm);
    else
        c=log_color_map(rs,minrs,maxrs,cm);
    end
    %tuv11 = cat(3,x,y);
    scatter(x,y,'+','MarkerEdgeColor',c);
    text(x+5/space,y,sprintf('%d',tt),'Color','k');
     rectangle('Position',[x-radius,y-radius,radius*2,radius*2],'Curvature',[1,1],'EdgeColor',c,'LineWidth',2);    
     %drawnow;
end
hold off;
title('singular features with their log responses as color index', ...
'FontSize', 14);
%colorbar;
drawnow;

function [c]=linear_color_map(rs,minrs,maxrs,cm)
if(maxrs-minrs<1e-3)
    irs=1;
else
    irs=round(((rs-minrs)/(maxrs-minrs))*size(cm,1));
end
if(irs<1)
    irs=1;
else
    if(irs>size(cm,1))
        irs=size(cm,1);
    end
end
c=cm(irs,:);

%
function [c]=log_color_map(rs,minrs,maxrs,cm)
if(log(maxrs)-log(minrs)<1e-3)
    irs=256;
else
    irs=round(((log(rs)-log(minrs))/(log(maxrs)-log(minrs)))*size(cm,1));
end
if(irs<1)
    irs=1;
else
    if(irs>size(cm,1))
        irs=size(cm,1);
    end
end
c=cm(irs,:);


